<template>
  <el-container class="home-container">
    <!--    头部区域-->
    <el-header>
      <div>
        <img src="../assets/images/12300.gif" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!--    页面主体区域-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏展开折叠 -->
        <div class="toggle-button" @click="toggleCollapase">|||</div>
        <el-menu background-color="#2e354d" text-color="#fff" active-text-color="#597ff8" :unique-opened="true"
                 :collapse="isCollapse" :collapse-transition='false' :router="true" :default-active="activePath">
          <!--一级菜单-->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!--一级菜单模板区域-->
            <template slot="title">
              <!--              <i class="el-icon-location"></i>-->
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{ item.authName }}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--      右边内容区域-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      //左侧菜单数据
      menulist: [],
      iconsObj: {
        201: 'iconfont icon-showpassword',
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju'
      },
      isCollapse: false,
      activePath: '',//保存路由链接激活状态
    }
  },
  components: {},
  methods: {
    loginout() {
      window.sessionStorage.clear()  //清空token window.sessionStorage.clear()
      this.$router.push('/login')  //跳转登录
    },
    // 请求左侧菜单所有数据
    async getMenuList() {
      const {data: res} = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.menulist = res.data
    },
    // 展开折叠
    toggleCollapase() {
      this.isCollapse = !this.isCollapse
    },
    // 保存激活的链接状态
    // 保存点击二级菜单的激活列表 保持菜单点击的链接 activePath 状态=左侧菜单点击高亮状态保持
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }

  },
}

</script>
<style scoped lang="less">
.el-header {
  background: #333744;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #dad9d9;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }

  img {
    width: 60px;
    height: 60px;
    //padding-top: 10px;
    //border: 1px solid green;
    //border-radius: 50%;
    //background: #363e5b;
  }
}

.el-aside {
  background: #2e354d;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background: #eaedf1;
}

.home-container {
  height: 100%;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background: #3e4b75;
  font-size: 10px;
  color: #dad9d9;
  text-align: center;
  line-height: 24px;
  letter-spacing: 0.3em;
  font-weight: bold;
  cursor: pointer;
}
</style>
